<?php
session_start();

// 检查用户是否已登录
if (!isset($_SESSION['username'])) {
    header('Location: login.php');
    exit();
}
require_once 'track_online.php';
// 当前用户信息
$currentUser = $_SESSION['username'];
$userDir = __DIR__ . "/user/$currentUser";

// 确保用户文件夹存在
if (!is_dir($userDir)) {
    mkdir($userDir, 0777, true);
}

// 用户信息文件路径
$infoFile = "$userDir/info.json";

// 默认字段
$defaultData = [
    'gender' => '',
    'birthplace' => '',
    'school' => '',
    'current_residence' => '',
    'zodiac_sign' => '',
    'birthday' => '',
    'bio' => '还没有填写介绍...',
];

// 如果文件不存在，则创建文件并写入默认值
if (!file_exists($infoFile)) {
    file_put_contents($infoFile, json_encode($defaultData));
}

// 加载用户信息
$userInfo = json_decode(file_get_contents($infoFile), true);

// 检查字段完整性，如果字段缺失，则自动补全
$updated = false;
foreach ($defaultData as $key => $value) {
    if (!isset($userInfo[$key])) {
        $userInfo[$key] = $value;
        $updated = true;
    }
}

// 如果字段有补全，则更新 info.json 文件
if ($updated) {
    file_put_contents($infoFile, json_encode($userInfo));
}

// 处理表单提交（更新信息）
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    foreach ($userInfo as $key => $value) {
        if (isset($_POST[$key])) {
            $userInfo[$key] = $_POST[$key];
        }
    }

    // 保存更新后的信息
    file_put_contents($infoFile, json_encode($userInfo));
    header('Location: profile.php'); // 保存后返回"我的页面"
    exit();
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>编辑用户信息 - JIIL微聊</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
        }
        
        :root {
            --primary-color: #4e54c8;
            --secondary-color: #6c63ff;
            --bg-color: #f8f9fa;
            --card-bg: #ffffff;
            --border-color: #e0e4e9;
            --text-color: #333333;
            --text-secondary: #6c757d;
            --shadow: 0 8px 20px rgba(0, 0, 0, 0.08);
            --success-color: #4CAF50;
        }
        
        body {
            background-color: var(--bg-color);
            color: var(--text-color);
            line-height: 1.6;
            min-height: 100vh;
        }
        
        /* 顶部栏 */
        .header {
            background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
            color: white;
            padding: 22px 25px;
            display: flex;
            align-items: center;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.12);
            position: sticky;
            top: 0;
            z-index: 100;
        }
        
        .back-btn {
            background: rgba(255, 255, 255, 0.2);
            border: none;
            color: white;
            width: 40px;
            height: 40px;
            border-radius: 50%;
            font-size: 18px;
            cursor: pointer;
            transition: all 0.3s ease;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        
        .back-btn:hover {
            background: rgba(255, 255, 255, 0.3);
            transform: translateX(-3px);
        }
        
        .header-title {
            font-size: 20px;
            font-weight: 600;
            margin-left: 20px;
        }
        
        /* 主容器 */
        .container {
            max-width: 700px;
            margin: 30px auto;
            padding: 0 20px;
        }
        
        /* 编辑卡片 */
        .edit-card {
            background: var(--card-bg);
            border-radius: 20px;
            padding: 40px;
            box-shadow: var(--shadow);
            margin-bottom: 30px;
            animation: fadeIn 0.4s ease;
        }
        
        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(20px); }
            to { opacity: 1; transform: translateY(0); }
        }
        
        .card-title {
            font-size: 26px;
            font-weight: 700;
            color: var(--primary-color);
            margin-bottom: 35px;
            display: flex;
            align-items: center;
            gap: 12px;
            position: relative;
            padding-left: 20px;
        }
        
        .card-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 3px;
            bottom: 3px;
            width: 5px;
            background: var(--primary-color);
            border-radius: 10px;
        }
        
        .form-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 25px;
        }
        
        /* 表单元素 */
        .form-group {
            margin-bottom: 22px;
            position: relative;
        }
        
        .form-group label {
            display: block;
            font-weight: 600;
            color: var(--text-color);
            margin-bottom: 10px;
            font-size: 16px;
            padding-left: 5px;
            display: flex;
            align-items: center;
            gap: 10px;
        }
        
        .form-group input, .form-group select, .form-group textarea {
            width: 100%;
            padding: 16px;
            font-size: 16px;
            border: 1px solid var(--border-color);
            border-radius: 12px;
            background: rgba(246, 248, 250, 0.6);
            transition: all 0.3s ease;
        }
        
        .form-group input:focus, .form-group select:focus, .form-group textarea:focus {
            border-color: var(--primary-color);
            box-shadow: 0 0 0 3px rgba(78, 84, 200, 0.15);
            outline: none;
            background: white;
        }
        
        .form-group textarea {
            resize: vertical;
            min-height: 120px;
        }
        
        .form-group .input-icon {
            position: absolute;
            right: 18px;
            top: 45px;
            color: var(--text-secondary);
            pointer-events: none;
        }
        
        /* 保存按钮 */
        .save-btn {
            background: var(--primary-color);
            background: linear-gradient(135deg, var(--primary-color) 0%, var(--secondary-color) 100%);
            color: white;
            border: none;
            padding: 18px;
            border-radius: 12px;
            font-size: 18px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            width: 100%;
            margin-top: 15px;
            box-shadow: 0 6px 15px rgba(78, 84, 200, 0.3);
        }
        
        .save-btn:hover {
            transform: translateY(-3px);
            box-shadow: 0 10px 20px rgba(78, 84, 200, 0.4);
        }
        
        .save-btn:active {
            transform: translateY(0);
        }
        
        /* 表单字段图标 */
        .form-icon {
            width: 30px;
            height: 30px;
            border-radius: 50%;
            background: linear-gradient(135deg, rgba(78, 84, 200, 0.1), rgba(108, 99, 255, 0.1));
            display: flex;
            align-items: center;
            justify-content: center;
            color: var(--primary-color);
        }
        
        /* 响应式调整 */
        @media (max-width: 768px) {
            .edit-card {
                padding: 25px;
            }
            
            .form-grid {
                grid-template-columns: 1fr;
            }
            
            .header {
                padding: 18px 20px;
            }
        }
        
        @media (max-width: 480px) {
            .container {
                padding: 0 15px;
            }
            
            .edit-card {
                padding: 20px 15px;
            }
            
            .card-title {
                font-size: 22px;
            }
            
            .form-group input, .form-group select, .form-group textarea {
                padding: 14px;
            }
        }
    </style>
</head>
<body>
    <!-- 顶部栏 -->
    <div class="header">
        <a href="profile.php" class="back-btn">
            <i class="fas fa-arrow-left"></i>
        </a>
        <div class="header-title">编辑个人资料</div>
    </div>
    
    <div class="container">
        <div class="edit-card">
            <h2 class="card-title">
                <i class="fas fa-user-edit"></i>
                个人信息
            </h2>
            
            <form method="POST">
                <div class="form-grid">
                    <div class="form-group">
                        <label>
                            <div class="form-icon">
                                <i class="fas fa-venus-mars"></i>
                            </div>
                            性别
                        </label>
                        <select id="gender" name="gender">
                            <option value="">请选择性别</option>
                            <option value="男" <?= $userInfo['gender'] === '男' ? 'selected' : '' ?>>男</option>
                            <option value="女" <?= $userInfo['gender'] === '女' ? 'selected' : '' ?>>女</option>
                            <option value="无性别人" <?= $userInfo['gender'] === '无性别人' ? 'selected' : '' ?>>无性别人</option>
                        </select>
                        <div class="input-icon">
                            <i class="fas fa-chevron-down"></i>
                        </div>
                    </div>
                    
                    <div class="form-group">
                        <label>
                            <div class="form-icon">
                                <i class="fas fa-calendar-day"></i>
                            </div>
                            出生日期
                        </label>
                        <input type="datetime-local" id="birthday" name="birthday" value="<?= htmlspecialchars($userInfo['birthday']) ?>">
                        <div class="input-icon">
                            <i class="fas fa-calendar-alt"></i>
                        </div>
                    </div>
                </div>
                
                <div class="form-grid">
                    <div class="form-group">
                        <label>
                            <div class="form-icon">
                                <i class="fas fa-home"></i>
                            </div>
                            出生地
                        </label>
                        <input type="text" id="birthplace" name="birthplace" value="<?= htmlspecialchars($userInfo['birthplace']) ?>" placeholder="例：北京市朝阳区">
                        <div class="input-icon">
                            <i class="fas fa-map-marker-alt"></i>
                        </div>
                    </div>
                    
                    <div class="form-group">
                        <label>
                            <div class="form-icon">
                                <i class="fas fa-building"></i>
                            </div>
                            现居住地
                        </label>
                        <input type="text" id="current_residence" name="current_residence" value="<?= htmlspecialchars($userInfo['current_residence']) ?>" placeholder="例：上海市浦东新区">
                        <div class="input-icon">
                            <i class="fas fa-location-dot"></i>
                        </div>
                    </div>
                </div>
                
                <div class="form-grid">
                    <div class="form-group">
                        <label>
                            <div class="form-icon">
                                <i class="fas fa-graduation-cap"></i>
                            </div>
                            学校
                        </label>
                        <input type="text" id="school" name="school" value="<?= htmlspecialchars($userInfo['school']) ?>" placeholder="请输入学校名称">
                        <div class="input-icon">
                            <i class="fas fa-school"></i>
                        </div>
                    </div>
                    
                    <div class="form-group">
                        <label>
                            <div class="form-icon">
                                <i class="fas fa-star"></i>
                            </div>
                            星座
                        </label>
                        <input type="text" id="zodiac_sign" name="zodiac_sign" value="<?= htmlspecialchars($userInfo['zodiac_sign']) ?>" placeholder="例：天蝎座">
                        <div class="input-icon">
                            <i class="fas fa-moon"></i>
                        </div>
                    </div>
                </div>
                
                <div class="form-group">
                    <label>
                        <div class="form-icon">
                            <i class="fas fa-user-pen"></i>
                        </div>
                        个人介绍
                    </label>
                    <textarea id="bio" name="bio" placeholder="简单介绍一下自己..."><?= htmlspecialchars($userInfo['bio']) ?></textarea>
                </div>
                
                <button type="submit" class="save-btn">
                    <i class="fas fa-save"></i> 保存个人信息
                </button>
            </form>
        </div>
    </div>
    
    <script>
        // 出生日期字段的自定义逻辑
        const birthdayField = document.getElementById('birthday');
        
        // 如果没有值，设置为今天的日期作为placeholder
        if (!birthdayField.value) {
            const now = new Date();
            const year = now.getFullYear();
            const month = (now.getMonth() + 1).toString().padStart(2, '0');
            const day = now.getDate().toString().padStart(2, '0');
            const hours = now.getHours().toString().padStart(2, '0');
            const minutes = now.getMinutes().toString().padStart(2, '0');
            
            birthdayField.value = `${year}-${month}-${day}T${hours}:${minutes}`;
        }
        
        // 保存按钮点击效果
        const saveBtn = document.querySelector('.save-btn');
        saveBtn.addEventListener('click', function() {
            this.innerHTML = '<i class="fas fa-spinner fa-spin"></i> 保存中...';
            this.disabled = true;
            
            // 模拟保存过程的延迟
            setTimeout(() => {
                document.querySelector('form').submit();
            }, 800);
        });
    </script>
</body>
</html>